<template>
  <div class="product-recommend-card" @click="handleClick">
    <!-- 商品图片 -->
    <div class="product-image">
      <img 
        :src="product.image" 
        :alt="product.name"
        @error="handleImageError"
        loading="lazy"
      />
      <!-- 折扣标签 -->
      <div v-if="discountPercent > 0" class="discount-badge">
        {{ discountPercent }}折
      </div>
    </div>

    <!-- 商品信息 -->
    <div class="product-info">
      <!-- 商品名称 -->
      <div class="product-name" :title="product.name">
        {{ product.name }}
      </div>

      <!-- 商品描述 -->
      <div v-if="product.description" class="product-description">
        {{ product.description }}
      </div>

      <!-- 评分和销量 -->
      <div v-if="product.rating || product.sales" class="product-meta">
        <div v-if="product.rating" class="rating">
          <div class="stars">
            <span 
              v-for="i in 5" 
              :key="i"
              class="star"
              :class="{ 'star--filled': i <= Math.floor(product.rating) }"
            >
              ★
            </span>
          </div>
          <span class="rating-text">{{ product.rating }}</span>
        </div>
        <div v-if="product.sales" class="sales">
          已售{{ formatSales(product.sales) }}
        </div>
      </div>

      <!-- 价格信息 -->
      <div class="product-price">
        <span class="current-price">¥{{ product.price.toFixed(2) }}</span>
        <span v-if="product.originalPrice && product.originalPrice > product.price" class="original-price">
          ¥{{ product.originalPrice.toFixed(2) }}
        </span>
      </div>

      <!-- 操作按钮 -->
      <div class="product-actions">
        <van-button 
          type="default" 
          size="small" 
          @click.stop="handleViewDetail"
          class="detail-btn"
        >
          查看详情
        </van-button>
        <van-button 
          type="primary" 
          size="small" 
          @click.stop="handleBuy"
          class="buy-btn"
        >
          立即购买
        </van-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { showToast } from 'vant'

// 商品类型定义
export interface ProductItem {
  id: string
  name: string
  price: number
  originalPrice?: number
  image: string
  description?: string
  rating?: number
  sales?: number
}

// Props
interface Props {
  product: ProductItem
}

const props = defineProps<Props>()

// Emits
const emit = defineEmits<{
  click: [product: ProductItem]
  buy: [product: ProductItem]
  viewDetail: [product: ProductItem]
}>()

// 计算属性
const discountPercent = computed(() => {
  if (!props.product.originalPrice || props.product.originalPrice <= props.product.price) {
    return 0
  }
  return Math.round((props.product.price / props.product.originalPrice) * 10)
})

// 方法
function handleClick(): void {
  emit('click', props.product)
}

function handleBuy(): void {
  emit('buy', props.product)
}

function handleViewDetail(): void {
  emit('viewDetail', props.product)
}

function handleImageError(event: Event): void {
  const img = event.target as HTMLImageElement
  img.src = ''
}

function formatSales(sales: number): string {
  if (sales >= 10000) {
    return `${(sales / 10000).toFixed(1)}万`
  } else if (sales >= 1000) {
    return `${(sales / 1000).toFixed(1)}k`
  }
  return sales.toString()
}
</script>

<style scoped>
.product-recommend-card {
  display: flex;
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  cursor: pointer;
  border: 1px solid #f0f0f0;
}

.product-recommend-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
}

.product-image {
  position: relative;
  width: 100px;
  height: 100px;
  flex-shrink: 0;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.product-recommend-card:hover .product-image img {
  transform: scale(1.05);
}

.discount-badge {
  position: absolute;
  top: 6px;
  left: 6px;
  background: linear-gradient(135deg, #ff6b6b, #ff5252);
  color: white;
  font-size: 10px;
  font-weight: bold;
  padding: 2px 6px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(255, 107, 107, 0.3);
}

.product-info {
  flex: 1;
  padding: 12px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.product-name {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  line-height: 1.4;
  margin-bottom: 4px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.product-description {
  font-size: 12px;
  color: #666;
  line-height: 1.3;
  margin-bottom: 6px;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.product-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 6px;
}

.rating {
  display: flex;
  align-items: center;
  gap: 4px;
}

.stars {
  display: flex;
  gap: 1px;
}

.star {
  font-size: 12px;
  color: #ddd;
  transition: color 0.2s ease;
}

.star--filled {
  color: #ffd700;
}

.rating-text {
  font-size: 11px;
  color: #666;
}

.sales {
  font-size: 11px;
  color: #999;
}

.product-price {
  display: flex;
  align-items: baseline;
  gap: 6px;
  margin-bottom: 8px;
}

.current-price {
  font-size: 16px;
  font-weight: bold;
  color: #ff6b6b;
}

.original-price {
  font-size: 12px;
  color: #999;
  text-decoration: line-through;
}

.product-actions {
  display: flex;
  gap: 8px;
}

.detail-btn {
  flex: 1;
  height: 28px;
  font-size: 12px;
  border: 1px solid #e0e0e0;
  color: #666;
}

.buy-btn {
  flex: 1;
  height: 28px;
  font-size: 12px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .product-recommend-card {
    border-radius: 8px;
  }
  
  .product-image {
    width: 80px;
    height: 80px;
  }
  
  .product-info {
    padding: 10px;
  }
  
  .product-name {
    font-size: 13px;
  }
  
  .product-description {
    font-size: 11px;
  }
  
  .current-price {
    font-size: 14px;
  }
  
  .product-actions {
    gap: 6px;
  }
  
  .detail-btn,
  .buy-btn {
    height: 26px;
    font-size: 11px;
  }
}

/* 深色模式支持 */
@media (prefers-color-scheme: dark) {
  .product-recommend-card {
    background: #2a2a2a;
    border-color: #404040;
  }
  
  .product-name {
    color: #e0e0e0;
  }
  
  .product-description {
    color: #b0b0b0;
  }
  
  .rating-text,
  .sales {
    color: #888;
  }
  
  .original-price {
    color: #888;
  }
  
  .detail-btn {
    border-color: #555;
    color: #b0b0b0;
    background: #333;
  }
}
</style>